var engine;
export default {
  setEngine(obj) {
    engine = obj;
  },

  loginList: null,
  logining: false,

  handleRequestLogin(callback) {
    if (callback) {
      if (!engine.loginList) {
        engine.loginList = [];
      }
      engine.loginList.push(callback);
    }
    if (engine.logining) {
      return;
    }
    engine.logining = true;
    wx.login({
      success(e) {
        console.log(e);
        // if (e.code) {
        //   engine.requestUrl(true, engine.getAppConfig().getUrl() + "/wxlogin/userinfobycode", "POST", {
        //     code: e.code
        //   }, true).then(function (data) {
        //     if (data) {
        //       if (data.id && data.id != "0" && data.wxSession) {
        //         engine.saveAccount({
        //           userId: data.id,
        //           token: data.wxSession,
        //           session: data.thirdSession,
        //           avatarUrl: data.avatarUrl,
        //           nickName: data.nickName,
        //           phone: data.phone
        //         });
        //         engine.loginCallback(data.id, data.wxSession, data.thirdSession);
        //       } else {
        //         engine.jumpPage("/pages/login/login", {
        //           tempToken: {
        //             thirdSession: data.thirdSession
        //           }
        //         });
        //       }
        //     } else {
        //       engine.toast("获取信息出错");
        //       engine.loginCallback();
        //     }
        //   }).catch(function (reason) {
        //     engine.toast(reason);
        //     engine.loginCallback();
        //   });
        // } else {
        //   engine.toast("微信登录异常");
        //   engine.loginCallback();
        // }
      },
      fail() {
        engine.toast("微信登录失败");
        engine.loginCallback();
      }
    });
  },

  login(tempToken, detail) {
    if (!tempToken || !detail) {
      engine.toast("微信授权失败");
      return;
    }
    if (detail.errMsg && (detail.errMsg.indexOf("auth deny") > -1 || detail.errMsg.indexOf("auth denied") > -1)) {
      engine.showScopeWarn("scope.userInfo", function (isScope) {
        if (isScope) {
          engine.toast("授权成功，请点击登录按钮");
        } else {
          engine.toast("微信授权失败");
        }
      });
      return;
    }
    let paramData = {};
    Object.assign(paramData, tempToken);
    Object.assign(paramData, detail);
    engine.requestUrl(true, engine.getAppConfig().getUrl() + "/wxlogin/userinfo", "POST", paramData, true).then(function (data) {
      if (data && data.id && data.id != "0" && data.wxSession) {
        engine.saveAccount({
          userId: data.id,
          token: data.wxSession,
          session: data.thirdSession,
          avatarUrl: detail.userInfo.avatarUrl,
          nickName: detail.userInfo.nickName,
          phone: null
        });
        engine.loginCallback(data.id, data.wxSession, data.thirdSession);
        engine.finishPage();
      } else {
        engine.toast("获取数据出错");
      }
    }).catch(function (reason) {
      engine.toast("获取数据失败");
    });
  },

  loginCallback(userCode, userAuthorization, session) {
    if (engine.loginList) {
      for (let i = 0; i < engine.loginList.length; i++) {
        let callback = engine.loginList[i];
        if (callback) {
          if (session) {
            callback({
              code: userCode,
              token: userAuthorization,
              session: session
            });
          } else {
            callback();
          }
        }
      }
      engine.loginList = null;
    }
    engine.logining = false;
  },

  loginClearTemp() {
    engine.loginCallback();
  },

  updateUserInfo(e, callback) {
    if (!callback) {
      return;
    }
    if (!e || !e.detail || !e.detail.rawData) {
      callback(false, "获取用户数据出错");
      return;
    }
    try {
      let userInfo = JSON.parse(e.detail.rawData);
      engine.requestUrl(true, engine.getAppConfig().getUrl() + "/wxlogin/update/userinfo", "POST", {
        nickName: userInfo.nickName,
        iconUrl: userInfo.avatarUrl,
        sex: userInfo.gender, // 0未知，1男，2女
        province: userInfo.province,
        city: userInfo.city
      }).then(function (data) {
        engine.saveMyName(userInfo.nickName);
        engine.saveMyIcon(userInfo.avatarUrl);
        callback(true);
      }).catch(function (reason) {
        callback(false, reason);
      });
    } catch (error) {
      callback(false, error);
    }
  },

  getAddressByLocation(longitude, latitude, callback) {
    engine.requestUrl(false, engine.getAppConfig().getDemandUrl() + "/addressbygps", "POST", {
      longitude: longitude,
      latitude: latitude
    }, true).then(data => {
      callback(true, data);
    }).catch(reason => {
      callback(false, reason);
    });
  },

  getRequestHeader(linkId) {
    let header = {
      "content-type": "application/json"
    };
    header.from = 7;
    header.type = 0;
    header.source = 1;
    if (engine.userCode && engine.userAuthorization) {
      header.authdst = engine.userCode;
      header.Authorization = engine.userAuthorization;
    }
    if (linkId) {
      header.linkid = linkId;
    }
    return header;
  },

  getUploadFileHeader() {
    let header = {
      "content-type": "multipart/form-data" // application/x-www-form-urlencoded
    };
    header.from = 7;
    header.type = 0;
    header.source = 1;
    if (engine.userCode && engine.userAuthorization) {
      header.authdst = engine.userCode;
      header.Authorization = engine.userAuthorization;
    }
    return header;
  }
}